<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addCateShowDialogBtnFn">添加分类</el-button>
      </div>
      <el-table :data="cateList" style="width: 100%" border stripe>
        <el-table-column type="index" label="序号" width="100"></el-table-column>
        <el-table-column prop="cate_name" label="分类名称"></el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
          <el-button type="primary" size="mini" @click="updateCateBtnFn(scope.row)">修改</el-button>
          <el-button type="danger" size="mini" @click="delCateFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="添加文章分类" :visible.sync="dialogVisible" width="30%" @close="dialogCloseFn">
      <el-form :model="addForm" :rules="addRules" ref="addRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getArtCateListAPI, addArtCateAPI, updateArtCateAPI, delArtCateAPI } from '@/api'

export default {
  name: 'ArtCate',
  data () {
    return {
      // 文章的分类列表
      cateList: [],
      dialogVisible: false,
      addForm: { // 添加表单的数据对象
        cate_name: '',
        cate_alias: ''
      },
      addRules: { // 添加表单的验证规则对象
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: 'blur' }
        ]
      },
      isEdit: false, // ture 为编辑 false为新增
      editId: ''
    }
  },
  methods: {
    // 删除文章分类
    async delCateFn (row) {
      const res = await delArtCateAPI(row.id)
      console.log(res)
      if (res.data.code === 0) {
        this.$message.success(res.data.message)
        this.getArtCateFn()
      } else {
        this.$message.error(res.data.message)
        this.getArtCateFn()
      }
    },
    // 修改文章分类按钮
    updateCateBtnFn (row) {
      this.isEdit = true
      this.editId = row.id
      this.dialogVisible = true
      this.$nextTick(() => {
        // 先让对话框出现, 它绑定空值的对象, 才能resetFields清空用初始空值
        this.addForm.cate_name = row.cate_name
        this.addForm.cate_alias = row.cate_alias
      })
    },
    // 获取文章分类
    async getArtCateFn () {
      const res = await getArtCateListAPI()
      if (res.data.code === 0) {
        this.cateList = res.data.data
      }
    },
    // 添加分类按钮
    addCateShowDialogBtnFn () {
      this.isEdit = false
      this.editId = ''
      this.dialogVisible = true
    },
    // 取消按钮
    cancelFn () {
      this.dialogVisible = false
    },
    // 确定按钮
    confirmFn () {
      this.$refs.addRef.validate(async valid => {
        if (valid) {
          if (this.isEdit) {
            // 要修改
            this.addForm.id = this.editId
            const res = await updateArtCateAPI(this.addForm)
            if (res.data.code === 0) {
              this.$message.success(res.data.message)
            } else {
              this.$message.error(res.data.message)
            }
          } else {
            const res = await addArtCateAPI(this.addForm)
            if (res.data.code === 0) {
              this.$message.success(res.data.message)
            } else {
              this.$message.error(res.data.message)
            }
          }
          this.getArtCateFn()
          this.dialogVisible = false
        } else {
          return false
        }
      })
    },
    // 关闭对话框的回调
    dialogCloseFn () {
      this.$refs.addRef.resetFields()
    }
  },
  mounted () {
    this.getArtCateFn()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
